using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;

public class MessageView : UiView
{
    private static MessageView instance;

    public static MessageView Instance=>instance;

    private MessageModel messageModel;

    private MessageData messageData;

    private Text messageTip;

    private CanvasGroup group;

    private float fadeTime = 3f;

    private void Awake()
    {
        SetUIConfig(UiConfigManger.GetConfig((int)ViewId.MessageUi));
        messageModel = GameModuleManager.Instance.GetOpenModule<MessageModel>();
        messageModel.messageView = this;
        messageModel.messageData = new MessageData();

        if (instance == null)
        {
            instance = this;
        }

        OnLoad();
        OnHide();

        messageTip = GetComponentInChildren<Text>();
        group = GetComponentInChildren<CanvasGroup>();
    }

    private void Start()
    {
        
    }

    private void Update()
    {
        
    }

    public override void Initialize()
    {
        
    }

    public override void SetModule(GameModule gameModule)
    {
        messageModel = gameModule as MessageModel;
    }

    public override void SetModuleData(object modelData)
    {
        messageData = modelData as MessageData;
    }

    public override void UpdateView()
    {
        
    }

    public void SetMessage(string message)
    {
        OnShow();
        messageTip.text = message;
        StartCoroutine(FadeOut());
        
    }

    private IEnumerator FadeOut()
    {
        while (group.alpha > 0)
        {
            group.alpha -= Time.deltaTime/fadeTime;
            yield return null;
        }

        OnHide();
        group.alpha = 1;
    }


}
